#!/usr/bin/env python
# -*- coding:utf-8 -*-
from __future__ import nested_scopes, generators, division, absolute_import, with_statement, print_function, \
    unicode_literals

import argparse
import yaml
import os

from service import Service
from const import *
from util import *

if __name__ == '__main__':
    # 解析命令行参数
    ap = argparse.ArgumentParser(description='a crawler model')
    ap.add_argument('paresrs',
                    nargs='*',
                    action=ParserAction,
                    help='Select pares want to use')
    ap.add_argument('-m', '--mask',
                    action='store_const',
                    const=None,
                    help='a mask, used when it stops')
    ap.add_argument('-d', '--debug',
                    action='store_true',
                    help='use debug mode')
    ap.add_argument('-c', '--config',
                    action='store_const',
                    const=None,
                    help='set config path(default static/crawler.yaml)')
    args = ap.parse_args("".split())
    if args.config is None:
        args.config = os.path.join(PATH, 'static/crawler.yaml')
    else:
        args.config = os.path.abspath(args.config)
    if args.mask is None:
        args.mask = 'crawler-default'

    # 解析配置文件
    conf = DEFAULT_CONFIG
    with open(args.config, 'r') as fp:
        conf.update(yaml.safe_load(fp))

    if args.mode == 'parser':
        parsers = {'parsers': [{'name': parser, 'worker': 1} for parser in args.parsers]}
        conf.update(parsers)

    # 启动服务
    svc = Service(conf, debug=args.debug, mask=args.mask)
    svc.start()
